{% extends 'base.html' %}
{% load myinclusion %}
{% block css %}
<link href="/static/css/bootstrapValidator.min.css" rel="stylesheet">
{% endblock %}
{% block content %}
<div class="right_col" role="main">
    <div class="">
        <div class="clearfix"></div>
        <div class="row">
            <div class="col-md-12 col-sm-12 col-xs-12">
                <div class="tabbable" id="tabs-754998">
                    <ul class="nav nav-tabs">
                        <li class="active">
                        <a href="#panel-single" data-toggle="tab">单机管理</a>
                        </li>
                        <li>
                        <a href="#panel-group" data-toggle="tab">批量管理</a>
                        </li>
                    </ul>
                    <div class="x_panel" style="border-top:none;">
                        <div class="x_title">
                            <h2>远程管理 </h2>
                            <ul class="nav navbar-right panel_toolbox">
                                <li><a class="collapse-link"><i class="fa fa-chevron-up"></i></a>
                                </li>
                                <li class="dropdown">
                                <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false"><i class="fa fa-wrench"></i></a>
                                <ul class="dropdown-menu" role="menu">
                                    <li><a href="#">Settings 1</a>
                                    </li>
                                    <li><a href="#">Settings 2</a>
                                    </li>
                                </ul>
                                </li>
                                <li><a class="close-link"><i class="fa fa-close"></i></a>
                                </li>
                            </ul>
                            <div class="clearfix"></div>
                        </div>
                        <div class="x_content">
                            <div class="loading"></div>
                            <div class="tab-content">
                                <div class="tab-pane active" id="panel-single">
                                    {% if perms.deploy.edit_deploy %}
                                    <fieldset>
                                    {% else %}
                                    <fieldset disabled="disabled">
                                    {% endif %}
                                    <div class="item form-group">
                                        <label class="level col-md-2 col-sm-2 col-xs-12" style="text-align:right;height:34px;line-height:34px;"><button name="plus" class="btn btn-sm btn-info" style="margin-right:0"><i class="fa fa-plus"></i></button>&nbsp;<button name="del" class="btn btn-sm btn-warning"><i class="fa fa-minus"></i></button></label>
                                        <label class="col-md-3 col-sm-3 col-xs-12" style="height:34px;line-height:34px;">目标主机</label>
                                        <label class="col-md-5 col-sm-5 col-xs-12" style="height:34px;line-height:34px;">远程命令</label>
                                        <label class="col-md-2 col-sm-2 col-xs-12" style="height:34px;line-height:34px;">失败跳过</label>
                                    </div>
                                    <form method="post" action="" class="form-horizontal form-border" enctype="multipart/form-data">
                                        {% csrf_token %}
                                        <div id="group0" class="item form-group">
                                            <label class="level col-md-2 col-sm-2 col-xs-12" style="text-align:right;height:34px;line-height:34px;">L0</label>
                                            <div class="col-md-3 col-sm-3 col-xs-12" style="padding-bottom:5px;">
                                                {% show_user_group_minions request.user.pk request.user.is_superuser 'single' %}
                                            </div>
                                            <div class="col-md-5 col-sm-5 col-xs-12" style="padding-bottom:5px;">
                                                <input type="text" class="form-control" name="arg" value="">
                                            </div>
                                            <div class="col-md-2 col-sm-2 col-xs-12" style="height:34px;line-height:34px;">
                                                <input type="hidden" name="ifcheck" value="0">
                                                <input type="checkbox" name="ifcheck" value="1">
                                            </div>
                                        </div>
                                        <div name="ajax_result" class="item form-group">
                                            <label class="control-label col-md-2 col-sm-3 col-xs-12">返回结果</label>
                                            <div class="col-md-9 col-sm-9 col-xs-12">
                                                <fieldset disabled>
                                                    <textarea class="form-control" name="result" rows="15">{{ ret }}</textarea>
                                                </fieldset>
                                            </div>
                                        </div>
                                        <div class="ln_solid"></div>
                                        <div class="form-group">
                                            <div class="col-md-6 col-md-offset-3">
                                                <span class="esingle">
                                                <a href="#" onclick="remote_exec(this, 'advance', '{% url 'advanced_manage' %}');" name="panel-single" role="button" class="btn btn-success">运行</a>
                                                </span>
                                                <a href="#" onclick="show_result();" role="button" class="btn btn-info">查看结果</a>
                                            </div>
                                        </div>
                                    </form>
                                    </fieldset>
                                </div>
                                <div class="tab-pane" id="panel-group">
                                    {% if perms.deploy.edit_deploy %}
                                    <fieldset>
                                    {% else %}
                                    <fieldset disabled="disabled">
                                    {% endif %}
                                    <div class="item form-group">
                                        <label class="level col-md-2 col-sm-2 col-xs-12" style="text-align:right;height:34px;line-height:34px;"><button name="plus" class="btn btn-sm btn-info" style="margin-right:0"><i class="fa fa-plus"></i></button>&nbsp;<button name="del" class="btn btn-sm btn-warning"><i class="fa fa-minus"></i></button></label>
                                        <label class="col-md-3 col-sm-3 col-xs-12" style="height:34px;line-height:34px;">目标主机</label>
                                        <label class="col-md-5 col-sm-5 col-xs-12" style="height:34px;line-height:34px;">远程命令</label>
                                        <label class="col-md-2 col-sm-2 col-xs-12" style="height:34px;line-height:34px;">失败跳过</label>
                                    </div>
                                    <form method="post" action="" class="form-horizontal form-border" enctype="multipart/form-data">
                                        {% csrf_token %}
                                        <div id="group0" class="item form-group">
                                            <label class="level col-md-2 col-sm-2 col-xs-12" style="text-align:right;height:34px;line-height:34px;">L0</label>
                                            <div class="col-md-3 col-sm-3 col-xs-12" style="padding-bottom:5px;">
                                                {% show_user_group_groups request.user.pk request.user.is_superuser %}
                                            </div>
                                            <div class="col-md-5 col-sm-5 col-xs-12" style="padding-bottom:5px;">
                                                <input type="text" class="form-control" name="arg" value="">
                                            </div>
                                            <div class="col-md-2 col-sm-2 col-xs-12" style="height:34px;line-height:34px;">
                                                <input type="hidden" name="ifcheck" value="0">
                                                <input type="checkbox" name="ifcheck" value="1">
                                            </div>
                                        </div>
                                        <div name="ajax_result" class="item form-group">
                                            <label class="control-label col-md-2 col-sm-3 col-xs-12">返回结果</label>
                                            <div class="col-md-9 col-sm-9 col-xs-12">
                                                <fieldset disabled>
                                                    <textarea class="form-control" name="result" rows="15">{{ ret }}</textarea>
                                                </fieldset>
                                            </div>
                                        </div>
                                        <div class="ln_solid"></div>
                                        <div class="form-group">
                                            <div class="col-md-6 col-md-offset-3">
                                                <input type="hidden" name="check_type" value="panel-group">
                                                <span class="egroup">
                                                <a href="#" onclick="remote_exec(this, 'advance', '{% url 'advanced_manage' %}');" name="panel-group" role="button" class="btn btn-success">运行</a>
                                                </span>
                                                <a href="#" onclick="show_result();" role="button" class="btn btn-info">查看结果</a>
                                            </div>
                                        </div>
                                    </form>
                                    </fieldset>
                                </div>
                            </div>

                            <div id="result"></div>
                            <!-- 模态框（Modal） -->
                            <div class="modal fade" id="result-modal-salt" tabindex="-1" role="dialog" aria-labelledby="result-modal" aria-hidden="true">
                                <div class="modal-loading"></div>
                                <div class="modal-dialog">
                                    <div class="modal-content" style="background-color:#272822;color:#00ff00;">
                                        <div class="modal-header" style="border-bottom:1px solid #666">
                                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                                            <b class="modal-title" id="result-modal" style="color:#f8f8f2">运行结果</b>&nbsp;
                                            <a href="#" onclick="result_check('exec', '{% url 'ajax_result' %}')" role="button" class="btn btn-dark btn-xs">重新查询</a>
                                            <a href="#" onclick="collapse(this);" state="0" role="button" class="btn btn-dark btn-xs">展开所有</a>
                                        </div>
                                        <div id="modal-body" class="modal-body">
                                            <div class="panel-group" id="accordion">
                                            </div>
                                        </div>
                                        <!--<div class="modal-footer">

                                        </div>-->
                                    </div><!-- /.modal-content -->
                                </div><!-- /.modal-dialog -->
                            </div><!-- /.modal -->

                            <input type="hidden" class="form-control" value="" id="jid">
                            <input type="hidden" class="form-control" value="" id="exec_type">

                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}
{% block js %}
<script src="/static/js/csrf.js"></script>
<script>
$(document).keypress(function(e){
    if(e.keyCode==13){
        var ev = document.all ? window.event : e;
        var v_id = $(ev.target).attr('id');
        $("."+v_id+" a")[0].click();
        e.preventDefault();
    }
});

function collapse(obj){
    var newstate = $(obj).attr('state') ^ 1, text = newstate ? "折叠":"展开";
    if(newstate==1){
        $(".panel-collapse").collapse('show');
    } else{
        $(".panel-collapse").collapse('hide');
    }
    $(obj).html(text + "所有");
    $(obj).attr('state', newstate);
}

function plus(type){
    var $div = $("#"+type+" div[id^='group']:last");
    var num = parseInt( $div.prop("id").match(/\d+/g), 10 ) + 1;
    var $group = $div.clone().prop("id", "group" + num);
    $div.after( $group );
    $("#" + type + " #group" + num + " .level").text("L" + num);
}
function del(type){
    var $div = $("#"+type+" div[id^='group']:last");
    var num = parseInt( $div.prop("id").match(/\d+/g), 10 );
    if( num == 0 ){
        return false;
    }
    $("#" + type + " #group" + num).remove();
}

function remote_exec(obj) {
    var f=$(obj).closest("form");
    var tgt_select=f.find("select[name='tgt_select']").val();
    var arg=f.find("input[name='arg']").val();
    if(tgt_select=="" || arg==""){
        $("#accordion").html("请选择主机/模块或者远程命令");
        $("#result-modal-salt").modal("show");
        return false;
    }
    $(".loading").show();
    $.ajax({
        type:"POST",
{#        url:"{% url 'ajax_exec' %}",#}
        data:f.serialize(),
        dataType:"html",
        success:function (ret) {
            $(".loading").hide();
            var ret=JSON.parse(ret);
            var html="";
            $.each(ret, function (i, item) {
                if(item.iftrue==0){
                    color = "66d9ef";
                }else if(item.iftrue==1){
                    color = "e6db74";
                }else{
                    color = "f92672";
                }
                $.each(item, function(k,v){
                    if(k!="iftrue"){
                        html = html + "<div class='panel panel-default' style='background:#272822;border:none;'><div><b style='color: #" + color
                            + "'>主机：<a data-toggle='collapse' data-parent='#accordion' href='#" +
                            k.split(':')[0] + "' style='color: #" + color + "'>" + k + "</a></b></div><b><div id='" + k.split(':')[0]
                            + "' class='panel-collapse collapse'><div><p class='mydashed'>结果：</b><br />" + v + "</p></div></div></div>";
                    }
                })
            })
            $("#accordion").html(html);
            $("#result-modal-salt").modal("show");
        }
    })
}

$(function(){
    $("#panel-single button[name='plus']").click(function(){
            plus("panel-single");
        })
    $("#panel-single button[name='del']").click(function(){
            del("panel-single");
        })
    $("#panel-single input[name='send']").click(function(){
            send("panel-single");
        })
    $("#panel-group button[name='plus']").click(function(){
            plus("panel-group");
        })
    $("#panel-group button[name='del']").click(function(){
            del("panel-group");
        })
    $("#panel-group input[name='send']").click(function(){
            send("panel-group");
        })
    })
</script>
{% endblock %}